﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace CTManagerSoln
{
    class clsLoaichungtu
    {

        internal static System.Data.DataTable getLoaiChungTu()
        {
            DataTable dt = new DataTable();
            string sql = "select MaLoaiChungTu,TenLoaiChungTu,Mota from tblLoaiChungTu";
            //using (OleDbConnection con = new OleDbConnection(Commons.conn.ConnectionString))
            //{
            //    OleDbDataAdapter adap = new OleDbDataAdapter(sql, con);
            //    adap.Fill(dt);
            //}
            OleDbConnection con = new OleDbConnection(Commons.DBConnect.ConnectionString);
            OleDbDataAdapter adap = new OleDbDataAdapter(sql, con);
            adap.Fill(dt);
            return dt;
        }

        internal static int Update(string maLoaiChungTu, string tenLoaiChungTu, string moTa)
        {
            int stateUpdate;
            string sql = "update tblLoaiChungTu set TenLoaiChungTu=@tenloaichungtu,Mota=@mota where MaLoaiChungTu=@maloaichungtu";
            using (OleDbConnection conn=new OleDbConnection(Commons.DBConnect.ConnectionString))
            {
                using (OleDbCommand command = new OleDbCommand(sql,conn))
                {
                    conn.Open();
                    command.Parameters.Add("@tenloaichungtu", OleDbType.VarChar).Value = tenLoaiChungTu;
                    command.Parameters.Add("@mota", OleDbType.VarChar).Value = moTa;
                    command.Parameters.Add("@maloaichungtu", OleDbType.VarChar).Value = maLoaiChungTu;
                   stateUpdate= command.ExecuteNonQuery();
                }   
            }
            return stateUpdate;
        }

        internal static DataTable getLoaiChungTu(string maLoaiChungTu)
        {
            DataTable dt = new DataTable();
            string sql =string.Format("select MaLoaiChungTu,TenLoaiChungTu from tblLoaiChungTu where MaLoaiChungTu='{0}'",maLoaiChungTu);
            
            OleDbConnection con = new OleDbConnection(Commons.DBConnect.ConnectionString);
            OleDbDataAdapter adap = new OleDbDataAdapter(sql, con);
            adap.Fill(dt);
            return dt;
        }

        internal static int Insert(string maLoaiChungTu, string tenLoaiChungTu, string moTa)
        {
            int stateUpdate;
            string sql = "INSERT into tblLoaiChungTu (MaLoaiChungTu,TenLoaiChungTu, Mota) Values(@maloaichungtu,@tenloaichungtu,@mota)";
            using (OleDbConnection conn = new OleDbConnection(Commons.DBConnect.ConnectionString))
            {
                using (OleDbCommand command = new OleDbCommand(sql, conn))
                {
                    conn.Open();
                    command.Parameters.Add("@maloaichungtu", OleDbType.VarChar).Value = maLoaiChungTu;
                    command.Parameters.Add("@tenloaichungtu", OleDbType.VarChar).Value = tenLoaiChungTu;
                    command.Parameters.Add("@mota", OleDbType.VarChar).Value = moTa;
                    stateUpdate = command.ExecuteNonQuery();
                }
            }
            return stateUpdate;
        }
    }
}
